(function() {
    Nokia.Drag = Nokia.Widget.extend({
        initialize: function(options) {
            var instance = this;

            var defaults = {
                element: ''
            };

            instance.options = jQuery.extend(defaults, options);

            instance._super.apply(instance, [instance.options]);

            var element = instance.options.element;

            instance.element = jQuery(element).css('position', 'absolute');

            instance._attachEvents();

            instance.registerVibrationOn(['create']);
            instance.fireCallback('create');
        },

        _attachEvents: function() {
            var instance = this;
            var element = instance.element;

            element.bind('mousedown', function(e) {
            	instance.startCoord = {x: e.pageX, y: e.pageY};
            	instance.doDrag = true;
				return false;
            });

            element.bind('mouseup', function() {
            	instance.doDrag = false;
				return false;
            });

            jQuery(document).bind('mousemove', function(e) {
                instance._registerMouseDelta(e.clientX, e.clientY);
				return false;
            });

		},

		_registerMouseDelta: function(x, y) {
			var instance = this;
			var element = instance.element;
			var startCoord = instance.startCoord;

			if ( instance.doDrag ) {
				element.css({top: startCoord.y + (y - startCoord.y - 10) + 'px', left: startCoord.x + (x - startCoord.x - 10) + 'px' });
			}

		}

    });
})();
